package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.PlayActivity;
import com.fanhao.dllo.soundfm.adapter.PlayListAdapter;
import com.fanhao.dllo.soundfm.entity.PlayListResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.utils.CountToOthers;
import com.fanhao.dllo.soundfm.volley.GetBitmap;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/15.
 * 播放列表,有悬浮窗的界面
 */
public class PlayListFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private LinearLayout linearPlayList;
    private ListView playLv;
    private PlayListAdapter playAdapter;
    private TextView tracksTv, collectPlayListTv, recomPlayListTv, titleHeaderTv, nameHeaderTv,
            descripHeaderTv, tagHeaderTv, playCountsHeaderTv, tracksNeckTv;
    private NetworkImageView bigHeaderIv;
    private Circlelmage smallHeaderIv;
    private ImageView backHeaderIv, shareHeaderIv;
    private View playListHeader;
    private long uId;
    private int tag;


    // 上一个界面传过来的uid值
    // 根据uid的不同所得的 url也就不同
    public PlayListFragment(long uId, int tag) {
        this.uId = uId;
        this.tag = tag;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_play_list;
    }

    @Override
    protected void initView() {
        linearPlayList = findView(R.id.linear_playlist);
        tracksTv = findView(R.id.tvTracks_playList);
        collectPlayListTv = findView(R.id.tvCollection_playList);
        recomPlayListTv = findView(R.id.tvRecomemd_playList);
        playLv = findView(R.id.listView_palyList);

        // 悬浮部分 neck
        // neck 是和linearPlayList部分相同的界面
        View neck = View.inflate(getContext(), R.layout.play_list_neck_item, null);
        // 头布局 header
        playListHeader = View.inflate(getContext(), R.layout.play_list_header_item, null);

        // 初始化头布局里的组件
        backHeaderIv = (ImageView) playListHeader.findViewById(R.id.ivBack_playList_header);
        shareHeaderIv = (ImageView) playListHeader.findViewById(R.id.ivShare_playList_header);
        titleHeaderTv = (TextView) playListHeader.findViewById(R.id.tvTitle_playList_header);
        nameHeaderTv = (TextView) playListHeader.findViewById(R.id.tvName_playList_header);
        descripHeaderTv = (TextView) playListHeader.findViewById(R.id.tvDescrip_playList_header);
        tagHeaderTv = (TextView) playListHeader.findViewById(R.id.tvTag_playList_header);
        playCountsHeaderTv = (TextView) playListHeader.findViewById(R.id.tvPlayCounts_playList_header);
        tracksNeckTv = (TextView) neck.findViewById(R.id.tvTracks_playList_neck);
        smallHeaderIv = (Circlelmage) playListHeader.findViewById(R.id.ivSmall_playList_header);
        bigHeaderIv = (NetworkImageView) playListHeader.findViewById(R.id.ivBig_playList_header);
        tracksTv = findView(R.id.tvTracks_playList);

        backHeaderIv.setOnClickListener(this);

        linearPlayList.bringToFront();
        playLv.addHeaderView(playListHeader);
        playLv.addHeaderView(neck);
        playLv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // 根据firstVisibleItem 的值来决定
                // 是否隐藏和显示悬浮部分
                if (firstVisibleItem == 0) {
                    // 当前第一个显示的item值是0时
                    // 即header显示时,linearPlayList隐藏
                    linearPlayList.setVisibility(View.GONE);
                } else {
                    linearPlayList.setVisibility(View.VISIBLE);
                }
            }
        });
        playAdapter = new PlayListAdapter(getActivity());
        playLv.setAdapter(playAdapter);
        playLv.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        String url = null;
        // 通过传过来的tag值来赋予不同的Url
        if (tag == 0) {
            // 小编推荐的url
            url = "http://mobile.ximalaya.com/mobile/others/ca/album/track/" +
                    uId + "/true/1/20?device=android";
        } else if (tag == 1) {
            // 听**的url
            url = "http://mobile.ximalaya.com/mobile/others/ca/album/track/" + uId + "/true/1/20?position=1&device=android&title=%E5%90%AC%E6%96%B0%E9%97%BB";
        }

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        PlayListResult plr = gson.fromJson(response,
                                PlayListResult.class);
                        if (plr != null) {
                            PlayListResult.AlbumEntity albumEntity = plr.getAlbum();
                            // 直接给header的控件赋值
                            if (albumEntity != null) {
                                titleHeaderTv.setText(albumEntity.getTitle());
                                nameHeaderTv.setText(albumEntity.getNickname());
                                descripHeaderTv.setText(albumEntity.getIntro());
//                                descripHeaderTv.setText(albumEntity.getd);
                                tagHeaderTv.setText(albumEntity.getTags());
                                playCountsHeaderTv.setText(CountToOthers.getCountToWan(albumEntity.getPlayTimes()));
                                bigHeaderIv.setImageUrl(albumEntity.getCoverLarge(), getImageLoader());
                                GetBitmap.getBitmap(albumEntity.getAvatarPath(), smallHeaderIv);
                                tracksTv.setText("共 :" + albumEntity.getTracks() + " 集");
                                tracksNeckTv.setText("共 :" + albumEntity.getTracks() + " 集");
                            }
                            PlayListResult.TracksEntity tracksEntity = plr.getTracks();
                            if (tracksEntity != null) {
                                List<PlayListResult.TracksEntity.ListEntity> tracksList =
                                        tracksEntity.getList();
                                Log.d("PlayList", "------->" + tracksList.size() + "");
                                if (tracksList != null && tracksList.size() > 0) {
                                    playAdapter.addTracksData(tracksList);
                                }
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack_playList_header:
                goBackToRight();
                break;
            case R.id.ivShare_playList_header:

                break;
            case R.id.tvDescrip_playList_header:

                break;
            case R.id.ivBig_playList_header:

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      if (position > 1){
        long uid = playAdapter.getTracksList().get(position - 2).getTrackId();
        Intent intent = new Intent();
        intent.setClass(getActivity(), PlayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putLong("trackId", uid);
        intent.putExtras(bundle);
        getActivity().startActivity(intent);
        getActivity().overridePendingTransition(R.anim.move_in_from_bottom, R.anim.no_move);

        }
    }
}
